/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai.opimage;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.renderable.RenderedImageFactory;
import javax.media.jai.ImageLayout;
import javax.media.jai.JAI;
import ca.forklabs.media.jai.operator.KMeansDescriptor;

/**
 * Class {@code KMeansRIF} is a {@link RenderedImageFactory} supporting the
 * <em>kmeans</em> operation in the rendered image layers.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.opimage.KMeansRIF">Daniel Léonard</a>
 * @version $Revision$
 * @see    KMeansOpImage
 */
public class KMeansRIF implements RenderedImageFactory {

//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public KMeansRIF() {
   // nothing
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the number of clusters.
    * @param   pb   the parameter block.
    * @return   the number of clusters.
    */
   protected int getClusters(ParameterBlock pb) {
      int index = KMeansDescriptor.CLUSTER_PARAMETER_INDEX;
      int clusters = pb.getIntParameter(index);
      return clusters;
      }

   /**
    * Gets the evaluation function.
    * @param    pb   the parameter block.
    * @return   the evaluation function.
    */
   protected KMeansDescriptor.EvaluationFunction getEvaluationFunction(ParameterBlock pb) {
      int index = KMeansDescriptor.EVALUATION_FUNCTION_PARAMETER_INDEX;
      KMeansDescriptor.EvaluationFunction function = (KMeansDescriptor.EvaluationFunction) pb.getObjectParameter(index);
      return function;
      }

   /**
    * Gets the number of iterations.
    * @param   pb   the parameter block.
    * @return   the number of iterations.
    */
   protected int getIterations(ParameterBlock pb) {
      int index = KMeansDescriptor.ITERATIONS_PARAMETER_INDEX;
      int iterations = pb.getIntParameter(index);
      return iterations;
      }

   /**
    * Gets the color map.
    * @param   pb   the parameter block.
    * @return   the color map.
    */
   protected int[][] getColorMap(ParameterBlock pb) {
      int index = KMeansDescriptor.COLOR_MAP_PARAMETER_INDEX;
      int[][] color_map = (int[][]) pb.getObjectParameter(index);
      return color_map;
      }


//---------------------------
// Implemented methods from java.awt.image.renderable.RenderedImageFactory
//---------------------------

   /**
    * Creates a new instance of {@link KMeansOpImage} in the rendered
    * layer.
    * @param   pb   the parameter block.
    * @param   hints   optional rendering hints.
    */
   @Override
   public RenderedImage create(ParameterBlock pb, RenderingHints hints) {
      RenderedImage source = pb.getRenderedSource(0);

      int clusters = this.getClusters(pb);
      KMeansDescriptor.EvaluationFunction function = this.getEvaluationFunction(pb);
      int iterations = this.getIterations(pb);
      int[][] color_map = this.getColorMap(pb);

   // this is exceptional, most factories are descendent of AbstractCRIF
      ImageLayout layout = (null == hints) ? null : (ImageLayout) hints.get(JAI.KEY_IMAGE_LAYOUT);

      RenderedImage sink = new KMeansOpImage(source, clusters, function, iterations, color_map, hints, layout);
      return sink;
      }

   }
